package com.wuxianedu.a242_animation;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by TED on 2016/9/28.
 */
public class TweenActivity extends AppCompatActivity {

    private TextView iv;
    private AlphaAnimation aa;
    private TranslateAnimation ta;
    private ScaleAnimation sa;
    private RotateAnimation ra;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        iv = (TextView) findViewById(R.id.tween_iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(TweenActivity.this,"抓住你",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 渐变
     *
     * @param view
     */
    public void alphaAction(View view) {
        //xml形式
        //AlphaAnimation animation = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha_animation);

        //java 代码形式
        // 0透明 1不透明
        aa = new AlphaAnimation(0, 1);
        aa.setDuration(2000);
        iv.startAnimation(aa);
    }

    /**
     * 平移
     *
     * @param view
     */
    public void translateAction(View view) {
        // float fromXDelta, float toXDelta, float fromYDelta, float toYDelta 其实呢，位置都要加上图片原来的坐标
        ta = new TranslateAnimation(100f, 300f, 100f, 300f);
//        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.5f,
//                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 1);
        // 播放的时长
        ta.setDuration(2000);
        //播放完毕后，停留在最后的画面
        ta.setFillAfter(true);
        ta.setInterpolator(new AccelerateDecelerateInterpolator());
        // 重复次数，从0开始
        ta.setRepeatCount(3);
        //Animation.REVERSE 往复  RESTART
        ta.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ta);
    }


    /**
     * 缩放
     * @param view
     */
    public void scaleAction(View view) {
//        ScaleAnimation animation = new ScaleAnimation(0.1f,4,0.1f,4);
//        ScaleAnimation animation = new ScaleAnimation(0.1f,4,0.1f,4,100,100);
        sa = new ScaleAnimation(0.1f,4,0.1f,4,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(2000);
        iv.startAnimation(sa);
    }


    /**
     * 旋转
     * @param view
     */
    public void rotateAction(View view) {
//        RotateAnimation animation = new RotateAnimation(0,360);
//        RotateAnimation animation = new RotateAnimation(0,360,100,100);
        ra = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setDuration(2000);
        iv.startAnimation(ra);
    }

    /**
     * 一起飞
     * @param view
     */
    public void flyAction(View view) {
        AnimationSet set = new AnimationSet(false);
        set.addAnimation(aa);
        set.addAnimation(ta);
        set.addAnimation(sa);
        set.addAnimation(ra);
        iv.startAnimation(set);
    }
}
